package edu.neu.ecommerceplatform.controller;

import edu.neu.ecommerceplatform.dto.CreateDeliverRequest;
import edu.neu.ecommerceplatform.dto.DeliverInfoResponse;
import edu.neu.ecommerceplatform.dto.UpdateDeliverRequest;
import edu.neu.ecommerceplatform.model.Deliver;
import edu.neu.ecommerceplatform.service.DeliverService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.net.URI;
import java.util.List;

@RestController
@RequestMapping("api/delivers")
public class DeliverController {

    private final DeliverService deliverService;

    @Autowired
    public DeliverController(DeliverService deliverService) {
        this.deliverService = deliverService;
    }

    @GetMapping
    public ResponseEntity<List<Deliver>> getAllDelivers() {
        return ResponseEntity.ok(deliverService.getAllDelivers());
    }

    @GetMapping("/{id}")
    public ResponseEntity<Deliver> getDeliverById(@PathVariable Integer id) {
        return ResponseEntity.ok(deliverService.getDeliverById(id));
    }

    @PostMapping
    public ResponseEntity<Deliver> createDeliver(@Valid @RequestBody CreateDeliverRequest request) {
        Deliver deliver = deliverService.createDeliver(request);
        URI location = ServletUriComponentsBuilder
                .fromCurrentRequest()
                .path("/{id}")
                .buildAndExpand(deliver.getId())
                .toUri();
        return ResponseEntity.created(location).body(deliver);
    }

    @PutMapping("/{id}")
    public ResponseEntity<Deliver> updateDeliver(@PathVariable Integer id, @Valid @RequestBody UpdateDeliverRequest request) {
        return ResponseEntity.ok(deliverService.updateDeliver(id, request));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteDeliver(@PathVariable Integer id) {
        deliverService.deleteDeliver(id);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/{id}/info")
    public ResponseEntity<DeliverInfoResponse> getDeliverInfoById(@PathVariable Integer id) {
        return ResponseEntity.ok(deliverService.getDeliverInfoById(id));
    }
} 